package com.todo.sight.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.sight.application.convert.SightInfoConvert;
import com.todo.sight.application.entity.bo.SightBaseBO;
import com.todo.sight.application.entity.bo.SightDetailBO;
import com.todo.sight.application.entity.bo.SightPoiBO;
import com.todo.sight.application.entity.condition.PoiConditionBO;
import com.todo.sight.application.frontend.service.SightInfoAggService;
import com.todo.sight.application.iface.frontend.request.QuerySightByCityAndNameParam;
import com.todo.sight.application.iface.frontend.request.QuerySightDetailByPoiParam;
import com.todo.sight.application.iface.frontend.request.QuerySightSummaryListByPoiIdParam;
import com.todo.sight.application.iface.frontend.response.SightDetailVO;
import com.todo.sight.application.iface.frontend.response.SightInfoVO;
import com.todo.sight.application.iface.frontend.response.SightSummaryVO;
import com.todo.sight.application.service.SightPoiService;
import com.todo.sight.application.service.SightService;
import com.todo.sight.common.enums.PoiBusinessTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (SightBase)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:48
 */
@Slf4j
@Service("sightInfoAggService")
public class SightInfoAggServiceImpl implements SightInfoAggService {

    @Resource
    private SightService sightService;
    @Resource
    private SightPoiService sightPoiService;

    @Override
    public BizPageResponse<SightInfoVO> querySightByCityAndName(QuerySightByCityAndNameParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        PoiConditionBO conditionBO = new PoiConditionBO();
        conditionBO.setCity(param.getCity());
        conditionBO.setName(param.getName());
        conditionBO.setPoiType(PoiBusinessTypeEnum.getByType(param.getPoiType()));
        conditionBO.setCurrentIndex(param.getCurrentIndex());
        conditionBO.setPageSize(param.getPageSize());
        conditionBO.setPageIndex(param.getPageIndex());
        PageResult<SightPoiBO> pageResult = sightPoiService.querySightPoiByCondition(conditionBO);
        if (pageResult == null) {
            log.warn("SightInfoAggService#querySightByCityAndName 根据城市和名称查询景点信息完成 数据为空 入参：【{}】", JSONObject.toJSONString(conditionBO));
            return BizPageResponse.success();
        }
        PageResult<SightInfoVO> result = new PageResult<>();
        result.setCurrentIndex(pageResult.getCurrentIndex());
        result.setPageCount(pageResult.getPageCount());
        result.setPageSize(pageResult.getPageSize());
        result.setTotal(pageResult.getTotal());
        result.setHasNext(pageResult.isHasNext());

        //POI数据查询不为空情况下去查询景点信息
        if (CollectionUtils.isNotEmpty(pageResult.getResults())) {
            List<Long> poiIdList = pageResult.getResults().stream().map(SightPoiBO::getId).collect(Collectors.toList());
            List<SightBaseBO> sightBaseBOList = sightService.querySightInfoByPoiIdList(poiIdList);
            Map<Long/*poiId*/, Long/*sightId*/> collectMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(sightBaseBOList)) {
                collectMap = sightBaseBOList.stream().collect(Collectors.toMap(SightBaseBO::getPoiId, SightBaseBO::getId));
            }
            result.setResults(SightInfoConvert.poiResponseconvertToSightInfoVOList(pageResult.getResults(), collectMap));
        }

        return BizPageResponse.success(result);

    }

    @Override
    public BizResponse<SightDetailVO> querySightDetailByPoi(QuerySightDetailByPoiParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        SightPoiBO sightPoiBO = sightPoiService.querySightPoiById(param.getPoiId());
        SightDetailBO sightDetailBO = sightService.querySightDetailByPoi(param.getPoiId());
        return BizResponse.success(SightInfoConvert.buildSightDetailVO(sightPoiBO, sightDetailBO));
    }

    @Override
    public BizResponse<List<SightSummaryVO>> querySightSummaryListByPoiId(QuerySightSummaryListByPoiIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<Long> collect = param.getPoiIdList().stream().map(Long::valueOf).collect(Collectors.toList());
        List<SightBaseBO> sightBaseBOList = sightService.querySightInfoByPoiIdList(collect);
        if (CollectionUtils.isEmpty(sightBaseBOList)) {
            return BizResponse.success(Collections.emptyList());
        }
        List<SightPoiBO> sightPoiBOList = sightPoiService.querySightPoiByIdList(collect);
        return BizResponse.success(SightInfoConvert.buildSightSummaryVOList(sightBaseBOList, sightPoiBOList));
    }
}
